# -*- coding: windows-1252 -*-
''' external service '''

import sys

_apiPath = 'C:\Data\package\Skype4Py-1.0.31.0'
_apiPathLin = '/dos/library/Skype4Py/Skype4Py-1.0.31.0'

sys.path.append(_apiPath)
sys.path.append(_apiPathLin)

import xmlrpclib
from SimpleXMLRPCServer import SimpleXMLRPCServer
import Skype4Py
import SocketServer
import os

global callStatus

# enable multithreading
 
class SimpleThreadedXMLRPCServer(SocketServer.ThreadingMixIn, SimpleXMLRPCServer):
	pass

def showSkypeWindow() :
	
	'''
	while call.Seen == False :
		pass
	'''
	
	try :
		call.StartVideoReceive()
	except : 
		print 'StartVideoReceive Error'
		
	try :	
		call.StartVideoSend()
	except : 
		print 'StartVideoSend Error'
	
	command = skype.Command('SET WINDOWSTATE MAXIMIZED')
	skype.SendCommand(command);
	
	skype.SendCommand(skype.Command("FOCUS"));

	# active skype window
	os.popen('SetSkypeToForeground.exe')

	#skype.Client.OpenContactsTab()
	
def hideSkypeWindow() :
	command = skype.Command('SET WINDOWSTATE HIDDEN')
	skype.SendCommand(command)
	
def onCall(call, status): 
	#call.StartVideoSend()
	#call.StartVideoReceive()	
	
	
	currentUser = skype.CurrentUser.Handle
	desc = call.PartnerHandle
	
	if currentUser != desc :
		if status == "INPROGRESS" :
			# showSkypeWindow()
			# hide xbmc window
			os.popen('SetXBMCWindowState.exe HIDE')
		
		elif status == 'FINISHED' :
			#hideSkypeWindow()
			# show xbmc window
			os.popen('SetXBMCWindowState.exe SHOW')
		
	# callStatus = str(status)
	
	print 'status ', status
	
def getCallStatus() :
	return callStatus

def startSkype():
	if not skype.Client.IsRunning :
		skype.Client.Start()
	skype.Client.Minimize()
	skype.OnCallStatus = onCall

def getAllFriend():
	contact = []
	friends = skype.Friends
	for friend in friends :
		user = {}
		user['SKYPE_NAME'] = friend.Handle
		user['STATUS'] = friend.OnlineStatus
		contact.append(user)
	return contact
	
	
def hangUp():
	calls = skype.ActiveCalls
	for call in calls :
		call.Finish()
	return 0
		
def call(skypeName):
	try :
		hangUp()
	except :
		pass
	
	import time
	
	skype.PlaceCall(skypeName)
	
	'''
	activeCommand = skype.Command('CALL ' + skypeName)

	finishCommand = skype.Command('BTN_RELEASE NO')
	callCommand = skype.Command('BTN_RELEASE YES')
	
	skype.SendCommand(activeCommand)
	
	print activeCommand
	
	time.sleep(1)
	skype.SendCommand(finishCommand)
	skype.SendCommand(callCommand)
	
	print 'finish'
	'''
	return  0

''' start here '''	
if __name__ == '__main__' :

	callIsFinished = set ([Skype4Py.clsFailed, Skype4Py.clsFinished, 
		Skype4Py.clsMissed, Skype4Py.clsRefused, Skype4Py.clsBusy, Skype4Py.clsCancelled]);
	skype = Skype4Py.Skype()

	startSkype()
	
	server = SimpleThreadedXMLRPCServer(('localhost', 8001))
	server.register_function(call)
	server.register_function(getAllFriend)
	server.register_function(hangUp)
	server.register_function(getCallStatus)
	print 'start service at port 8001'
	server.serve_forever()

